import colors from "./color";


/**
 * 巡检结果状态 文字 及 颜色
 * @param {} result 
 */
export function inspectionResultTextOrColor(result) {
	if (result === 1) {
		return {
			style: {
				color: colors.black
			},
			text: '正常'
		}
	}
	return {
		style: {
			color: colors.red,
		},
		text: '异常'
	}
}



export function arrayToTree(data, field = {
	ufield: 'id',
	pfield: 'parent_id',
	sub: 'children'
}) {
	data.forEach(item => {
		delete item[field.sub];
	})

	const map = {};
	data.forEach(item => {
		map[item[field.ufield]] = item;
	})
	const list = [];
	data.forEach(item => {
		let parent = map[item[field.pfield]];
		if (parent) {
			(parent[field.sub] || (parent[field.sub] = [])).push(item);
		} else {
			list.push(item)
		}
	})
	return list;
}
/**
 * 去重
 */
export function uniq(arr) {
	return Array.from(new Set(arr))
}

export const Factory = 'factory'
export const Hospital = 'hospital'

/**
 * 获取指定的Storage 值
 */
function getStorageValue(name) {
	return wx.getStorageSync(name)
}

/**
 * 获取 是否是厂家 标识
 */
export function Storage_IsFactoryValue() {
	//0否1是
	let val = getStorageValue('isFactory')
	let name = Number(val) ? Factory : Hospital
	return name
}
/**
 * 获取是否是厂家
 */
export function Storage_IsFactory() {

	let val = Storage_IsFactoryValue()
	return val === Factory
}

/**
 * 获取当前登录用户信息
 */
export function Storage_UserInfo() {
	return getStorageValue('pc_userInfo')
}

/**
 * 获取权限
 */
export function Storage_Permission() {
	return getStorageValue('permissions')
}

/**
 * 设置登陆 和 刷新之后的storage 
 * 
 */
export function Set_BaseStorage(res) {
	console.log(res, 'Set_BaseStorage');
	let isHxey = false
	if (res.edition?.tag) {
		isHxey = res.edition.tag === 'hxey'

	}
	//0否1是
	uni.setStorageSync('isFactory', res.body?.isFactory || 0)
	uni.setStorageSync('hxey', isHxey)
	uni.setStorageSync('token', res.body.token)
	uni.setStorageSync('depts', res.body.depts)
	if (res.body.depts.length > 0) {
		uni.setStorageSync('currentDept', res.body.depts[0])
	}
	uni.setStorageSync('imgUrl', res.body.WX_PREVIEW_FILE_BASE_URL)
	uni.setStorageSync('username', res.body.user)
	uni.setStorageSync('phone', res.body.phone)
	uni.setStorageSync('permissions', res.body.permissions)
	uni.setStorageSync('pc_userInfo', {
		phone: res.body.phone,
		userName: res.body.user,
		id: res.body.userId,
	})
	uni.$emit('update_storage', {
		msg: 'storage更新'
	})
}

export function theIdParentPath(list, value) { 
	let paths = []
	if (list.length && value) {
		const dg = (id) => {
			list.forEach((item) => { 
				if (item.id === id  ) {
					paths.push(item.id)
					dg(item.parent_id)
				}
			})
		}
		dg(value)
	} 
	return paths.join(',')||""


}

 